package com.ruoyi.dataserver.service.impl;

import com.ruoyi.dataserver.dto.GhlineDto;
import com.ruoyi.dataserver.model.GHLINE;
import com.ruoyi.dataserver.service.IGhAnalyzeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @ProjectName
 * @Package com.lab.dataserver.service.impl
 * @ClassName GhAnalyzeServceImpl
 * @Description 规划分析逻辑层
 * @Company lab
 * @Author wangzhx
 * @Date 2022/3/11  11:55
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Slf4j
@Component
public class GhAnalyzeServceImpl implements IGhAnalyzeService {

    @Autowired
    public JdbcTemplate jdbcTemplate;




    @Override
    public String add(GhlineDto dto) {
        try{
            RowMapper<GHLINE> rowMapper  = new BeanPropertyRowMapper<GHLINE>(GHLINE.class);
            String sqlSearch = "select * from GHLINE where name = '"+dto.getName()+"'";
            List<GHLINE> lineList = jdbcTemplate.query(sqlSearch,rowMapper);
            if(lineList.size() > 0){
                return "repeat";
            }else{
                String sql = "INSERT INTO GHLINE (name, startpoint,endpoint,qdgc,zdgc,qdms,zdms,gj,cz,mslx,dm) VALUES " +
                        "('"+dto.getName()+"'," +
                        "'"+dto.getStartpoint()+"'," +
                        "'"+dto.getEndpoint()+"'," +
                        "'"+dto.getQdgc()+"'," +
                        "'"+dto.getZdgc()+"'," +
                        "'"+dto.getQdms()+"'," +
                        "'"+dto.getZdms()+"'," +
                        "'"+dto.getGj()+"'," +
                        "'"+dto.getCz()+"'," +
                        "'"+dto.getMslx()+"'," +
                        "'"+dto.getDm()+"'" +
                        ")";
                jdbcTemplate.execute(sql);
                return "success";
            }
        } catch (Exception e) {
            return "fail";
        }


    }

    @Override
    public String delete(String id) {
        try {
            String sql = "delete from GHLINE where id = '"+id+"'";
            jdbcTemplate.execute(sql);
            return "success";
        }
        catch (Exception e) {
            return "fail";
        }
    }
    @Override
    public List<GHLINE> search() {
        RowMapper<GHLINE> rowMapper  = new BeanPropertyRowMapper<GHLINE>(GHLINE.class);
        String sqlLine = "select * from GHLINE  where 1 = 1 order by id desc";
        List<GHLINE> lineList = jdbcTemplate.query(sqlLine,rowMapper);
        return lineList;
    }
    @Override
    public GHLINE queryById(String id) {
        RowMapper<GHLINE> rowMapper  = new BeanPropertyRowMapper<GHLINE>(GHLINE.class);
        String sqlLine = "select * from GHLINE  where id = '"+id+"'";
        GHLINE line = jdbcTemplate.queryForObject(sqlLine,rowMapper);
        return line;
    }
}
